#!/bin/sh

pdockerpath="/data/docker"
pregistry="reg.klxedu.com"

echo "停止Docker服务"
systemctl stop docker

echo "卸载旧版本Docker"
yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine docker-ce

echo "安装dockers远程仓库"
yum install -y yum-utils device-mapper-persistent-data lvm2 && yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

echo "关闭防火墙"
systemctl stop firewalld.service && systemctl disable firewalld.service

echo "安装Docker"
yum install -y docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io

echo "启动Docker服务"
systemctl start docker

echo "设置Docker服务为自启动"
systemctl enable docker

echo "修改配置文件"
mkdir -p /data/docker
cat >/etc/docker/daemon.json<<EOF
{
  "registry-mirrors": ["https://hq4kxtot.mirrorg.aliyuncs.com"],
  "insecure-registries": ["${pregistry}"],
  "graph": "${pdockerpath}",
  "bip":"192.168.100.1/24",
  "exec-opts": ["native.cgroupdriver=systemd"],
  "max-concurrent-downloads": 10
}
EOF

echo "重新加载配置并重启"
systemctl daemon-reload && systemctl restart docker

pip=`ifconfig eth0 |awk -F '[ :]+' 'NR==2{print $3}'`

echo "配置阿里云k8s yum源"
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
EOF

yum -y install epel-release && yum clean all && yum makecache

echo "禁用SELinux"
setenforce 0 && sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config

echo "开启iptable转发"
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
EOF

sysctl --system

echo "在线安装k8s"
yum install -y kubelet-1.15.2-0 kubeadm-1.15.2-0 kubectl-1.15.2-0 && systemctl enable --now kubelet

echo "配置kubelet的cgroups、国内镜像代理"
DOCKER_CGROUPS=$(docker info | grep 'Cgroup' | cut -d' ' -f3)

cat >/etc/sysconfig/kubelet<<EOF
KUBELET_EXTRA_ARGS="--cgroup-driver=$DOCKER_CGROUPS --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1"
EOF

echo "启动"
systemctl daemon-reload && systemctl start kubelet

echo "禁用swap"
swapoff -a

echo "创建masterconfig.yaml配置文件"
cat >./masterconfig.yaml<<EOF
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: v1.15.2
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
controlPlaneEndpoint: "${pip}:6443"
networking:
  podSubnet: 10.244.0.0/16
controllerManager:
  extraArgs:
    bind-address: 0.0.0.0
scheduler:
  extraArgs:
    address: 0.0.0.0
EOF

echo "初始化Master节点"
kubeadm config images pull --config masterconfig.yaml && kubeadm init --config masterconfig.yaml --upload-certs >> ~/k8s.log

echo "请记住以上的输出信息，工作节点需要使用"

echo "配置访问权限"
mkdir -p $HOME/.kube && sudo cp -i /etc/kubernetes/admin.conf  $HOME/.kube/config && sudo chown $(id -u):$(id -g) $HOME/.kube/config

echo "安装网络插件"
kubectl apply -f kube-flannel.yml

echo "设置master为工作节点"
kubectl taint nodes --all node-role.kubernetes.io/master-

echo "命令行补全"
yum install -y bash-completion 
source /usr/share/bash-completion/bash_completion 
echo "source <(kubectl completion bash)" >> ~/.bashrc